package kit.edu.lego.gruppe3.levels.sumpf;

import lejos.nxt.LCD;
import lejos.util.Timer;
import lejos.util.TimerListener;

/**
 * Counts the light and dark areas
 * @author Lac
 *
 */
public class SumpfLineCrossed 
{
	
	public static final int LIGHT_GROUND_COUNT = 4; // sumpf is light
	public static final int DARK_GROUND_COUNT = 4;
	private Timer timer = null;
	
	public void resetValues()
	{
		lightAreasCrossed = 0;
		darkAreasCrossed = 0;
		lastCrossingWasDark = true;
	}

	private int lightAreasCrossed = 0;
	private int darkAreasCrossed = 0;
	
	private boolean lastCrossingWasDark = true;
	
	private static SumpfLineCrossed instance = null;
	
	private SumpfLineCrossed()
	{
		lightAreasCrossed = 0;
		darkAreasCrossed = 0;
	}
	
	public static SumpfLineCrossed getInstance()
	{
		if (instance != null)
		{
			return instance;
		} else 
		{
			instance = new SumpfLineCrossed();
			return instance;
		}
	}
	
	public void crossLightArea()
	{
		
		lightAreasCrossed++;
		lastCrossingWasDark = false;
		LCD.drawString("Light crossed "+lightAreasCrossed, 0,4);
	}
	
	public void crossDarkArea()
	{
		darkAreasCrossed++;
		lastCrossingWasDark = true;
		LCD.drawString("Dark crossed "+darkAreasCrossed, 0,5);
		
		if (darkAreasCrossed + 1 == DARK_GROUND_COUNT)
		{
			timer = new Timer(1, new TimerListener()
			{

				@Override
				public void timedOut()
				{
					if (DARK_GROUND_COUNT > darkAreasCrossed)
					{
						
						darkAreasCrossed++;
						lightAreasCrossed = LIGHT_GROUND_COUNT;
						System.out.println("Timer end: D:"+darkAreasCrossed+" L:"+lightAreasCrossed);
					}
					timer.stop();
				}
				
			});
			timer.start();
			
		}
	}
	
	public boolean isSumpfFinished(){
		return lightAreasCrossed >= LIGHT_GROUND_COUNT && darkAreasCrossed >= DARK_GROUND_COUNT;
	}
	
	/**
	 * @param isFinished if true all the areas are marked as crossed, else all 
	 * the areas are market as 0 times crossed
	 */
	public void setIsSumpfFinished(boolean isFinished){
		if (isFinished){
			lightAreasCrossed = LIGHT_GROUND_COUNT;
			darkAreasCrossed = DARK_GROUND_COUNT;
		} else {
			lightAreasCrossed = 0;
			darkAreasCrossed = 0;
		}
	}
	
	public void setLightAreasCrossed(int lightAreasCrossed) {
		this.lightAreasCrossed = lightAreasCrossed;
	}
	public int getLightAreasCrossed() {
		return lightAreasCrossed;
	}
	public void setDarkAreasCrossed(int darkAreasCrossed) {
		this.darkAreasCrossed = darkAreasCrossed;
	}
	public int getDarkAreasCrossed() {
		return darkAreasCrossed;
	}

	public boolean lastCrossingWasDark() {
		return lastCrossingWasDark;
	}
}
